[file_setsize]
	file_setsize Version 1.0b1 -- Created by Johan Sšlve <mailto:johan@montania.se>
	An alternate implementation for the file_setsize tag to overcome
	a security problem with that tag.
	When increasing a file's size, the extra space is filled with blanks instead 
	of just picking up stale data from the disk as the original tag does. 

	This macro overrides the default Lasso tag so no existing Lasso code needs to be changed. 
	(When Lasso is starting up the console window says 
	"Duplicate 'file_setsize' tag found. Overriding previous tag.". This is normal.) 

	The macro should work just like the original tag except for the "stale data" problem.

	Usage example: [File_SetSize:'fileName', 20]
	
	Limitation: due to the loop limit of max 1000 loop iterations in Lasso, 
	this implementation of [file_Setsize] can increase a file's size by 100099 bytes maximum.

#MACRO_BEGIN
[lasso_comment]
[Macro_Parent: #MACRO_INSERT_PARAMS#]
		[var_set: 'macro_filesetsize_filename'=(macro_param: 1, EncodeNone)]
		[var_set: 'macro_filesetsize_getsize'=(file_getsize: 
			(var: 'macro_filesetsize_filename', EncodeNone))]
		[var_set: 'macro_filesetsize_setsize'=(macro_param: 2, EncodeNone)]
		[if: (file_exists: (var: 'macro_filesetsize_filename', EncodeNone))]
			-- only waste energy on this if the file actually exists
			
			[if: (var: 'macro_filesetsize_setsize', EncodeNone) 
				> (var: 'macro_filesetsize_getsize', EncodeNone)]
				-- increase file size - write blanks to the file (this is the improvement)
	
				[var_set: 'macro_filesetsize_changesize'=(Math_Sub: 
					(var: 'macro_filesetsize_setsize', EncodeNone), 
					(var: 'macro_filesetsize_getsize', EncodeNone))]
	
				-- first write blanks in chunks of 100 to fill up more efficiently
				[loop: (math_floor: 
					(math_div: (var: 'macro_filesetsize_changesize', EncodeNone), '100.00'))]
					[File_Write: (var: 'macro_filesetsize_filename', EncodeNone),
						(String_Concatenate: EncodeNone, 
							'          ',
							'          ',
							'          ',
							'          ',
							'          ',
							'          ',
							'          ',
							'          ',
							'          ',
							'          ')]
				[/loop]
				
				-- write single blanks to fill up to the end
				[loop: (math_mod: (var: 'macro_filesetsize_changesize', EncodeNone), '100')]
					[File_Write: (var: 'macro_filesetsize_filename', EncodeNone),' ']
				[/loop]
			[else]
				-- decrease file size (recreate previous functionality
					 - can't use file_setsize for this since we're overriding that tag)
				[var_set: 'macro_filesetsize_temp_file'=(File_Read: EncodeNone, 
					(var: 'macro_filesetsize_filename', EncodeNone),
					FileStartPos='0',FileEndPos=(var: 'macro_filesetsize_setsize', EncodeNone))]
				[File_Write: (var: 'macro_filesetsize_filename', EncodeNone), 
					(var: 'macro_filesetsize_temp_file', EncodeNone), fileoverwrite]
			[/if]
		[/if]
				
		-- clear out variables
		[var_set: 'macro_filesetsize_temp_file'='',
			'macro_filesetsize_filename'='',
			'macro_filesetsize_setsize'='',
			'macro_filesetsize_getsize'='',
			'macro_filesetsize_changesize'='']
	
[/Macro_Parent]
[/lasso_Comment]
#MACRO_END
